$(function() {
    $('form').parsley();
    dynamicBinding();
    getAllResStatistics();
});
//遍历所有resStatistics输出到表格
function getAllResStatistics() {
    let deleteStr = "";
    let updateStr = "";
    let columns = [
        {title: "序号", data: null,"render": function (data,type,row,meta) {
                return data=Number(meta.row)+Number(1);
            }},
        {title: "区域", data: "area"},
        {title: "类型", data: "type"},
        {title: "所属单位", data: "departmentId"},
        {title: "时间", data: "time"},
        {title: "实现总数", data: "totalRealized"},
        {title: "类型总数", data: "totalType"},
        {title: "功能类总数", data: "totalNumber"},
        {title: "占比", data: "accountFor"},
        {title: "操作", data: "id","orderable": false, // 禁用排序
            "defaultContent": "","render": function (data,type,row,meta) {
                if (hasDeletePermission) {
                    deleteStr = "<button data-id=" + data + "  type='button' class='btn btn-danger data-delete'>删除</button>&emsp;"
                }
                if (hasUpdatePermission) {
                    updateStr = "<button data-id=" + data + " type='button' class='btn btn-info data-update' data-toggle='modal' data-target='#updateResStatisticsModal'>更新</button>";
                }
                return data = deleteStr+updateStr;}}
    ];
    let url ='/resStatistics/getAllResStatistics';
    dataTableInit(url,columns);
}
//动态绑定
function dynamicBinding() {
    //为所有的delete按钮绑定delete事件
    deleteBind( '/resStatistics/deleteResStatistics');
    //为所有的update按钮绑定update事件
    $("#datatable").on('click', ".data-update", function () {
        let data_id = $(this).attr('data-id');
        //获取此行数据
        let row = $("#datatable").DataTable().row($(this).parents('tr')).data();
        //遍历更新表单的所有拥有name的输入框，将行数据填充到这些输入框中，file除外
        $('#toUpdateResStatistics'+" [name]").each(function () {
            if($(this).attr('type')!=="file"){
                let name =  $(this).attr("name");
                $(this).val(row[name]);
            }
        });
        $('#toUpdateResStatistics').parsley().on('form:validate', function () {
        }).on('form:submit', function () {
            let id = data_id;
            let data = new FormData($('#toUpdateResStatistics')[0]);
            data.set("id", id);
            data.set("time",Date($(" #updateTime").val()));
            updateAjax(data, '/resStatistics/updateResStatistics', false, false, "#updateResStatisticsModal");
            return false;
        });
    });
    //为所有的add按钮绑定add事件
    $('#toAddResStatistics').parsley().on('form:validate', function () {
    }).on('form:submit', function () {
        let data = new FormData($('#toAddResStatistics')[0]);
        data.set("time",Date($(" #addTime").val()));
        addAjax(data, '/resStatistics/addResStatistics', false, false, "#addResStatisticsModal");
        return false;
    });
    //为add模态框绑定消失事件-清空模态框中内容
    $('#addResStatisticsModal').on('hidden.bs.modal', function () {
        document.getElementById("toAddResStatistics").reset();
    });
    //为update模态框绑定消失事件-清空模态框中内容
    $('#updateResStatisticsModal').on('hidden.bs.modal', function () {
        document.getElementById("toUpdateResStatistics").reset();
    });
}
